package com.chinasoft.maysham.manager.controller.user;

import com.chinasoft.maysham.manager.controller.BaseController;
import com.chinasoft.maysham.manager.model.User;
import com.chinasoft.maysham.manager.request.UserRegisterReq;
import com.chinasoft.maysham.manager.service.user.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController extends BaseController {

    @Resource
    private UserService userService;


    @PostMapping("/register")
    public User register(@RequestBody UserRegisterReq registerReq) {
        return userService.register(registerReq, currentUserId());
    }

    @PutMapping("/{userId}/update")
    public User update(@PathVariable String userId, @RequestBody UserRegisterReq registerReq) {
        return userService.update(userId, registerReq, currentUserId());
    }

    @GetMapping("/list")
    public List<User> list(@RequestParam(defaultValue = "1", required = false) int page,
                           @RequestParam(defaultValue = "10", required = false) int size,
                           @RequestParam(defaultValue = "", required = false) String keyword) {
        return userService.list(page, size, keyword, currentUserId());
    }

    @DeleteMapping("/{userId}/un_register")
    public Boolean unRegister(@PathVariable String userId) {
        return userService.unRegister(userId, currentUserId());
    }

    @PostMapping("/login")
    public User login() {
        return userService.login(currentUser().getUsername(), currentUser().getPassword());
    }
}
